/*
 */

package com.jams.goodox.activity.serve;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.entity.Neighborhood;
import com.jams.goodox.entity.User;
import com.jams.goodox.entity.resp.UserLisResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.RentingService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.ImageUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.TitleSetting;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.UserUtils;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by jams on 2016/6/6.
 * ***
 * Tel: ***
 * 邻里——加入成员列表
 */
public class JoinPersonsListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.lv_persons_list)
    RefreshRecyclerView refreshRecyclerView;
    @Bind(R.id.empty_view)
    LoadView emptyView;
    @Bind(R.id.sdv_join_list)
    SwipeRefreshLayout sdvJoinList;
    private List<UserLisResp.DataBean.ResultListBean> joinList;
    private TextView title;
    private int curPage;
    private Neighborhood circleItemBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_join_persons_list;
    }

    @Override
    protected void init() {
        setTitleVisibility(false);
        circleItemBean = (Neighborhood) getIntent().getSerializableExtra("CircleItemBean");
        title = (TextView) findViewById(R.id.tv_title_bar_title);
        joinList = new ArrayList<>();
        TitleSetting.titleBackKey(this, "");
        sdvJoinList.setOnRefreshListener(this);
        TextView headerView = new TextView(this);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                (int) getResources().getDimension(R.dimen.gap_little));
        headerView.setLayoutParams(layoutParams);
        refreshRecyclerView.setLayoutManager(new RefreshLinearLayoutManager(this));
        refreshRecyclerView.setHasFixedSize(true);
        refreshRecyclerView.setAdapter(new RecycleCommonAdapter<UserLisResp.DataBean.ResultListBean>(this, joinList, R.layout.join_person_list_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final UserLisResp.DataBean.ResultListBean item, int position) {
                helper.setText(R.id.tv_join_person_name_item, item.getUserSimple().getNickName());
                helper.setSimpleDraweeView(R.id.sdv_portrait_join_person_item, StringUtils.newString(ImageUtils.getSmallUrl(item.getUserSimple().getHeadPicName())));

                if (item.getUserSimple().getId().equals(SePref.getUserId())) {
                    helper.getView(R.id.iv_join_person_message).setVisibility(View.GONE);
                } else {
                    helper.getView(R.id.iv_join_person_message).setVisibility(View.VISIBLE);
                }
                if ("2".equals(item.getUserSimple().getSex())) {
                    helper.getView(R.id.iv_sex_join_person_item).setBackgroundResource(R.mipmap.female_icon);
                } else if ("1".equals(item.getUserSimple().getSex())) {
                    helper.getView(R.id.iv_sex_join_person_item).setBackgroundResource(R.mipmap.male_icon);
                } else {
                    helper.getView(R.id.iv_sex_join_person_item).setBackgroundResource(R.mipmap.none_sex);
                }
                helper.getView(R.id.iv_join_person_message).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (UserUtils.isUnLinkCompany()) {
                            ToastUtil.showLong(JoinPersonsListActivity.this, getString(R.string.un_check_pass));
                            return;
                        }
                        message(item.getUserSimple());
                    }
                });
            }
        });
        refreshRecyclerView.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                getJoinList();
            }
        });
        onRefresh();
    }

    //私信
    private void message(User userInfo) {
    }

    //获取加入人员列表
    private void getJoinList() {
        enqueue(createCallService(RentingService.class).getJoinList("neighbor", circleItemBean.getId(), curPage, 20), new RetrofitCallbackListener<UserLisResp>() {
            @Override
            public void onSuccess(Call<UserLisResp> call, UserLisResp result) {
                sdvJoinList.setRefreshing(false);
                refreshRecyclerView.loadMoreComplete();
                if (curPage == 1) {
                    joinList.clear();
                }
                refreshRecyclerView.setCanLoadMore(result.getData().getPageInfo().isHasMore());
                joinList.addAll(result.getData().getResultList());
                setTitleString(result.getData().getPageInfo().getTotal());
                curPage++;
                refreshRecyclerView.notifyData();
                if (!CollectionUtils.hasData(joinList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_common,"暂无相关内容");
                } else {
                    emptyView.loadComplete();
                }
            }

            @Override
            public void onError(String msg) {
                if (CollectionUtils.hasData(joinList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_common,"暂无相关内容");
                } else {
                    emptyView.loadComplete();
                }
                sdvJoinList.setRefreshing(false);
                refreshRecyclerView.loadMoreComplete();
            }
        });
    }

    @OnClick(R.id.iv_title_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void setTitleString(int counts) {
        title.setText(counts + "人点赞");
    }

    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                sdvJoinList.setRefreshing(true);
                curPage = 1;
                getJoinList();
            }
        });
    }
}
